import Vue from 'vue'
import VueRouter from 'vue-router'
import {
  getToken
} from '@/utils/auth'
import Layout from '../views/layout.vue'
import Home from '../views/home/index.vue'
import Course from '../views/course/index.vue'
import Training from '../views/training/index.vue'
import Teach from '../views/teach/index.vue'
import TrainingStatic from '../views/training/components/StaticIndex.vue'
import TeachStatic from '../views/teach/components/StaticIndex.vue'
import Topic from '../views/topic/index.vue'
import Expert from '../views/expert/index.vue'
import Personal from '../views/personal/index.vue'
import { toLogin } from '@/utils/baseConfig'

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    children: [{
        path: '/home',
        name: 'home',
        component: Home,
      },
      {
        path: '/training',
        name: 'training',
        component: TrainingStatic
      },
      {
        path: '/teach',
        name: 'teach',
        component: TeachStatic
      },
      {
        path: '/trainingStatic',
        name: 'trainingStatic',
        component: Training
      },
      {
        path: '/teachStatic',
        name: 'teachStatic',
        component: Teach
      },
      {
        path: '/course',
        name: 'course',
        component: Course
      },
      {
        path: '/topic',
        name: 'topic',
        component: Topic
      },
      {
        path: '/expert',
        name: 'expert',
        component: Expert
      },
      {
        path: '/personal',
        name: 'personal',
        meta: {
          requiresAuth: true
        },
        component: Personal
      },
      {
        path: '/trainingDetails',
        name: 'trainingDetails',
        component: () => import( /* webpackChunkName: "trainingDetails" */ '../views/trainingDetails/index.vue')
      },
      {
        path: '/courseDetails',
        name: 'courseDetails',
        meta: {
          requiresAuth: true
        },
        component: () => import( /* webpackChunkName: "courseDetails" */ '../views/courseDetails/index.vue')
      },
      {
        path: '/expert/detail',
        name: 'expertDetail',
        component: () => import( /* webpackChunkName: "expertDetail" */ '../views/expert/detail.vue')
      },
      {
        path: '/topic/detail',
        name: 'topicDetail',
        component: () => import( /* webpackChunkName: "topicDetail" */ '../views/topic/detail.vue')
      },
      {
        path: '/teachDetails',
        name: 'teachDetails',
        component: () => import( /* webpackChunkName: "teachDetails" */ '../views/teachDetails/index.vue')
      },
      {
        path: '/news',
        name: 'news',
        component: () => import( /* webpackChunkName: "news" */ '../views/news/index.vue')
      },
      {
        path: '/news/detail',
        name: 'newsDetail',
        component: () => import( /* webpackChunkName: "newsDetail" */ '../views/news/detail.vue')
      },
      {
        path: '/about',
        name: 'about',
        component: () => import( /* webpackChunkName: "about" */ '../views/about/index.vue')
      },
      {
        path: '/trainingVideoDetails',
        name: 'trainingVideoDetails',
        component: () => import( /* webpackChunkName: "trainingVideoDetails" */ '../views/trainingDetails/videoDetails.vue')
      },
      {
        path: '/teachVideoDetails',
        name: 'teachVideoDetails',
        component: () => import( /* webpackChunkName: "teachVideoDetails" */ '../views/teachDetails/videoDetails.vue')
      },
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( /* webpackChunkName: "login" */ '../views/login/index.vue')
  },
  {
    path: '/h5Live',
    name: 'h5Live',
    component: () => import( /* webpackChunkName: "h5Live" */ '../views/h5Live/index.vue')
  }
]

const router = new VueRouter({
  mode: 'history', // 尝试使用history模式
  base: process.env.NODE_ENV === 'development' ? '/' : '/',
  scrollBehavior: (to, from, savedPosition) => {
    if (savedPosition) {
      return savedPosition
    } else {
      return { y: 0 }
    }
  },
  routes
})

router.beforeEach((to, from, next) => {
  const requiresAuth = to.matched.some(record => !!record.meta.requiresAuth);
  const isAuthenticated = !!getToken();

  if (requiresAuth && !isAuthenticated) {
    // 如果访问的路由需要 Token，但用户没有登录，则重定向到登录页面
    toLogin()
    // next();
  } else if (to.path === '/login' && isAuthenticated) {
    // 如果用户已经登录，但尝试访问登录页面，则重定向到主页
    next('/');
  } else {
    next(); // 确保总是调用 next()
  }
});

export default router